package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.EvaluationProject;

/**
 * @desc   测评项目实体操作
 * @author ningqian
 * @date   2016-7-4
 */
@Service
public class EvaluationProjectService extends BaseDao<EvaluationProject>{

	/**
	 * 按条件查询
	 * @param evaluationProject
	 * @return
	 */
	public List<EvaluationProject> query(EvaluationProject evaluationProject, String... date) {
		if(null == evaluationProject) return super.findAll();
		if(evaluationProject.getiDisplayLength() > 0){
		  return super.find(createFilter(evaluationProject, date),evaluationProject.getiDisplayStart(),evaluationProject.getiDisplayLength());
		}
		return super.find(createFilter(evaluationProject, date));
	}

	public Long count(EvaluationProject evaluationProject, String... date) {
		return super.count(createFilter(evaluationProject, date));
	}
	public Long count(EvaluationProject examOptions){
		return super.count(createFilter(examOptions));
	}

	/**
	 * 拼装条件(名称、适用年龄段、创建时间)
	 * @param evaluationProject
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(EvaluationProject evaluationProject, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(!StringUtil.isNullOrEmpty(evaluationProject.getName())){
		  dc.add(Restrictions.like("name", evaluationProject.getName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(evaluationProject.getLiveId())){
		  dc.add(Restrictions.eq("liveId", evaluationProject.getLiveId()));
	  }
	  
	  if(!StringUtil.isNullOrEmpty(evaluationProject.getAgeGroup())){
		  dc.add(Restrictions.like("ageGroup", evaluationProject.getAgeGroup().trim(), MatchMode.ANYWHERE));
	  }
	  
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("insertTime", date[0],date[1]));
	  }
	  
		return dc;
	}
	
}
